package cn.itcast.sms.action;

import cn.itcast.crm.domain.Customer;
import cn.itcast.sms.utils.BaseAction;
import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.ws.rs.core.MediaType;

@Controller
@Namespace("/")
@ParentPackage("json-default")
@Scope("protoType")
public class LoginAction extends BaseAction<Customer> {

    //登陆的功能
    @Action(value = "customer_login",results = {@Result(name="login",type = "redirect",location = "index.html#/myhome"),
                                                @Result(name="fail",type = "redirect",location="login.html")})
    public String login(){
        System.out.println(model.getUsername());
        System.out.println(model.getPassword());
        Customer customer = WebClient.create("http://localhost:8081/crm_management/service/customerService/login?username="+model.getUsername()+"&password="+model.getPassword()).
                accept(MediaType.APPLICATION_JSON).type(MediaType.APPLICATION_JSON).get(Customer.class);
        if(customer != null){
            //登陆成功,将用户保存在session中
            ServletActionContext.getRequest().getSession().setAttribute("customer",customer);
            return "login";
        }else{

            return "fail";
        }

    }
}
